use  "$replication\data\IHDS.dta", clear


global depvars "bhuvan_dry winter_cropping  ndvi_delta_r_"
global ln_depvars ""
foreach var in $depvars {
	gen `var'_missing=(`var'==.)
	replace `var'=0 if `var'==.
	if "`var'"=="ndvi_delta_r_" {
		gen ln_`var'=ln(`var')
		replace ln_`var'=0 if `var'<=0
	}
	else  {
		gen ln_`var'=ln(1+`var')
	}
	global ln_depvars "$ln_depvars ln_`var'"
}

global depvars_wet "bhuvan_wet ndvi_delta_k_ evi_delta_k_"
global ln_depvars_wet ""
foreach var in $depvars_wet  {
	gen `var'_missing=(`var'==.)
	replace `var'=0 if `var'==.
	if "`var'"=="ndvi_delta_r_" {
		gen ln_`var'=ln(`var')
		replace ln_`var'=0 if `var'<=0
	}
	else  {
		gen ln_`var'=ln(1+`var')
	}
	global ln_depvars_wet "$ln_depvars_wet ln_`var'"
}


icw_index ln_bhuvan_dry ln_ndvi_delta_r_  ln_winter  , gen(icw_irrigation)


gegen obs_in_s_imp=max(obs_in_s_eXr), by(STATEID)


foreach var in ln_bhuvan_dry ln_ndvi_delta_r_  ln_winter ln_bhuvan_wet ln_ndvi_delta_k_ ln_evi_delta_k_ {
	qui sum `var'
	gen Z_`var'=(`var'-r(mean))/r(sd)
}

icw_index Z_ln_bhuvan_wet Z_ln_ndvi_delta_k_ Z_ln_evi_delta_k_ , gen(icw_wet)

foreach var of varlist icw* {
	sum `var'
	replace `var'=(`var'-r(mean))/r(sd)
}	


estimates clear

global reg_options "[iw=obs_in_s_imp], robust"

foreach var in $depvars {
	reg ln_rabi_share ln_`var' if `var'_missing==0 $reg_options
	estimates store `var'
}
	
reg ln_rabi_share $ln_depvars *_missing  $reg_options
estimates store ln_reg
	
reg ln_rabi_share icw_irrigation  $reg_options
estimates store ln_reg_i

estimates table _all, drop(*_missing _cons) se stats(N r2_a)
estimates table _all, drop(*_missing _cons) star(.1 .05 .01) stats(N r2_a)

